<template>
  <!-- 轮播图背景 -->
  <div class="background" ref="background">
    <div class="container">
      <el-carousel
        :interval="5000"
        height="420px"
        @change="handlerChange"
        :pause-on-hover="false"
        trigger="click"
      >
        <el-carousel-item v-for="item in imgList" :key="item.id">
          <img v-lazy="item.url" class="pointer" />
        </el-carousel-item>
      </el-carousel>
    </div>
    <span v-show="false">{{ imgList }}</span>
  </div>
</template>

<script name="Banner">
import { defineComponent, onUpdated, ref } from 'vue';
export default defineComponent({
  name: 'Banner', // 轮播图
  props: {
    imgList: {
      type: Array,
      default: () => [],
    },
  },
  setup(props) {
    const background = ref(null);
    let currentImgIndex = ref(0);
    function handlerChange(current) {
      currentImgIndex.value = current || 0;
      background.value.style.backgroundImage =
        'url(' + props.imgList[currentImgIndex.value].url + ')';
    }

    onUpdated(() => {
      handlerChange(0);
    });

    return {
      handlerChange,
      currentImgIndex,
      background,
    };
  },
});
</script>

<style lang="scss" scoped>
// 轮播图背景
.background {
  width: 100%;
  height: 420px;
  background-size: cover;
  background-repeat: no-repeat;
  position: relative;
  transition: all 0.5s;
  overflow: hidden;

  &::before {
    content: '';
    position: absolute;
    z-index: 0;
    width: 100%;
    height: 100%;
    display: block;
    backdrop-filter: blur(100px);
  }

  .container {
    height: 420px;
    margin: 0 auto;
  }
}

.el-carousel__item img {
  width: 100%;
}

.el-carousel__item:nth-child(2n) {
  background-color: #99a9bf;
}

.el-carousel__item:nth-child(2n + 1) {
  background-color: #d3dce6;
}
</style>
